package battleship;

import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;

public class User {

    int id;
    String username;

    public boolean online;
    public Date lastSeen;
    public boolean playing;
    public boolean inLobby;
    public boolean waitingForOpponent;

    LinkedBlockingQueue<Game> newOpenGames;
    LinkedBlockingQueue<Game> newRunningGames;
    LinkedBlockingQueue<Game> oldOpenGames;
    LinkedBlockingQueue<Game> oldRunningGames;

    Game game;
    LinkedBlockingQueue<User> waitForJoin;
    public boolean startingPlayer;
    public int opponentID;
    boolean donePlacingShips;
    LinkedBlockingQueue<User> waitForOppShips;
    public ArrayList<ArrayList<int[]>> ships;
    public ArrayList<int[]> hit;
    public ArrayList<int[]> sunk;
    LinkedBlockingQueue<int[]> lastShot;
    LinkedBlockingQueue<User> gameOver;

    public User() {
        System.out.println("User: creating a new user");
        online = true;
        playing = false;
        username = "";
        generateID();
        newOpenGames = new LinkedBlockingQueue<Game>();
        newRunningGames = new LinkedBlockingQueue<Game>();
        oldOpenGames = new LinkedBlockingQueue<Game>();
        oldRunningGames = new LinkedBlockingQueue<Game>();
        lastSeen = new Date();
        game = new Game(); // gets overwritten afterwards
        inLobby = true;
        waitForJoin = new LinkedBlockingQueue<User>();
        startingPlayer = false; // gets overwritten afterwards
        waitingForOpponent = false; // gets overwritten afterwards
        donePlacingShips = false;
        waitForOppShips = new LinkedBlockingQueue<User>();
        ships = new ArrayList<ArrayList<int[]>>();
        hit = new ArrayList<int[]>();
        sunk = new ArrayList<int[]>();
        lastShot = new LinkedBlockingQueue<int[]>();
        gameOver = new LinkedBlockingQueue<User>();

        checkIfOnline();
        System.out.println("User: finished creating user");
    }

    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    protected void generateID() {
        this.id = Context.nextUserID++;
    }

    public void checkIfOnline() {
        OnlineCheckThread thread = new OnlineCheckThread(this);
        thread.start();
    }

}

class OnlineCheckThread extends Thread {
    
    User user;
    
    public OnlineCheckThread(User user) {
        super();
        this.user = user;
    }

    public void run() {
        while(true) {
            try {
                if (new Date().getTime() - this.user.lastSeen.getTime() > 10000) {
                    user.online = false;
                    user.waitingForOpponent = false;
                    Context.onlineUsers.remove(user);
                    user.newOpenGames.clear();
                    user.newRunningGames.clear();
                    user.oldOpenGames.clear();
                    user.oldRunningGames.clear();
                    System.out.println("going to stop this thread!");
                    this.interrupt();
                    return;
                }
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("thread sleep interrupted: " + e.getMessage());
            }
        }
    }
}